package com.etc.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.etc.dao.CommentMapper;
import com.etc.entity.Comment;
import com.etc.service.CommentService;
import com.etc.util.PageData;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper cm;

    @Override
    public boolean deleteByPrimaryKey(Integer comId) {
        // TODO Auto-generated method stub
        return cm.deleteByPrimaryKey(comId) > 0;
    }

    @Override
    public boolean insert(Comment comment) {
        // TODO Auto-generated method stub
        return cm.insert(comment) > 0;
    }

    @Override
    public boolean insertSelective(Comment comment) {
        // TODO Auto-generated method stub
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        comment.setConDate(simpleDateFormat.format(new Date()));
        return cm.insertSelective(comment) > 0;
    }

    @Override
    public Comment getByPrimaryKey(Integer comId) {
        // TODO Auto-generated method stub
        return cm.getByPrimaryKey(comId);
    }

    @Override
    public boolean updateByPrimaryKeySelective(Comment comment) {
        // TODO Auto-generated method stub
        return cm.updateByPrimaryKeySelective(comment) > 0;
    }

    @Override
    public boolean updateByPrimaryKey(Comment comment) {
        // TODO Auto-generated method stub
        return cm.updateByPrimaryKey(comment) > 0;
    }

    @Override
    public PageData<Comment> getByPage(Integer page, Integer limit, String content) {
        // 得到起始位置
        int start = (page - 1) * limit;
        // 得到记录总条数
        int count = cm.countComment(content);
        // 得到分页和模糊查询后的数据
        List<Comment> list = cm.getByPage(start, limit, content);
        List<Comment> newComment = new ArrayList();
        //遍历集合，如果得到的用户不为空，即该用户存在则加进新的集合里
        for (Comment comment : list) {
            if (comment.getUser() != null) {
                newComment.add(comment);
            }
        }
        PageData<Comment> pg = new PageData<>(newComment, count, limit, page, content);
        return pg;
    }


    @Override
    public List<Comment> getByTraid(Integer traId) {
        // TODO Auto-generated method stub
        List<Comment> list = cm.getByTraid(traId);
        List<Comment> newComment = new ArrayList();
        //遍历集合，如果得到的用户不为空，即该用户存在则加进新的集合里
        for (Comment comment : list) {
            if (comment.getUser() != null) {
                newComment.add(comment);
            }
        }
        return newComment;
    }

    @Override
    public int countCommentByTraid(Integer traId) {
        return cm.countCommentByTraid(traId);
    }
}
